package homework;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.Calendar;

public class Homework {
    //递归遍历目录下所有文件路径
    public static void showDir(File file) {
        File file1[] = file.listFiles();
        for (File file2 : file1) {
            if (file2.isFile() && file2.getName().endsWith(".java")) {
                //输出符合条件的文件路径
                System.out.println(file2.getAbsolutePath());
            } else if (file2.isDirectory()) {
                //目录，递归
                showDir(file2);
            }
        }
    }

    public static void main(String[] args) throws ParseException, IOException {
        //1 使用大数运算，就算 4444444.4444/3.333333的值，保留2位小数，四舍五入
        BigDecimal b1 = new BigDecimal(4444444.4444);
        BigDecimal b2 = new BigDecimal(3.333333);
        BigDecimal result = b1.divide(b2, 2, RoundingMode.HALF_UP); //保留2位，四舍五入
        System.out.println(result.toString());
        System.out.println("------------------------------------");
        //2  输入你生日的前一周的周四是该年的第几天？
        Calendar calendar = Calendar.getInstance();
        //暂时用常量测试
        int year = 2000;
        int month = 11;
        int date = 2;
        calendar.set(year, month - 1, date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        while (dayOfWeek != 0) {//倒退回本周第一天
            calendar.add(Calendar.DATE, -1);
            dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        }
        calendar.add(Calendar.WEEK_OF_MONTH, -1);//退到上一周
        calendar.add(Calendar.DATE, 4);//周四
        System.out.println(calendar.get(calendar.YEAR) + "年" + (calendar.get(calendar.MONTH) + 1) + "月" + calendar.get(calendar.DATE) + "日" + "是该年" + calendar.get(Calendar.DAY_OF_YEAR) + "天");
        System.out.println("------------------------------------");
        //3、递归输出src目录中的所有.java文件的绝对路径。
        File fileJava = new File("day4/src");
        showDir(fileJava);
        //4、在电脑D盘下创建一个文件为HelloWorld.txt文件，判断他是文件还是目录，再创建一个目录IOTest，之后将HelloWorld.txt拷贝到IOTest目录的a.txt去。
        System.out.println("------------------------------------");
        File file = new File("D:\\HelloWorld.txt");
        file.createNewFile();//创建文件
        System.out.println(file.getName() + "是否为文件：" + file.isFile());
        File file2 = new File("D:\\IOTest");
        file2.mkdir();//创建目录
        Writer wr = new FileWriter("D:\\IOTest\\a.txt");
        Reader rd = new FileReader("D:\\HelloWorld.txt");
        char c[] = new char[4];
        int len = 0;
        while ((len = rd.read(c)) != -1) {
            wr.write(c, 0, len);
        }
        wr.close();
        rd.close();
        //5、从utf-8编码格式的文本复制到GBK编码的文本（不能出现乱码）
        System.out.println("------------------------------------");
        InputStreamReader isr=new InputStreamReader(new FileInputStream("D:\\IOTest\\a.txt"),"utf-8");
        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("\\IOTest\\b.txt"),"GBK");
        char bs[]=new char[20];
        len=0;
        while(((len = isr.read(bs)) !=-1)){
            osw.write(new String(bs,0,len));
        }
        osw.close();
        isr.close();
    }
}